package com.biz.primus.app.rest.handler.impl;

import com.biz.primus.app.rest.handler.BuildRequestHandler;
import com.biz.primus.base.exception.BizBusinessException;
import com.biz.primus.base.vo.address.BaseRequestVo;
import com.biz.primus.base.global.GlobalParams;
import com.biz.primus.base.vo.handler.GlobalParamsAware;
import com.biz.primus.base.vo.handler.IRequestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局参数初始化处理器
 *
 * @author jun.liu
 * @usage
 * @reviewer
 * @since 2017年11月3日
 */
@Component
@Slf4j
public class GlobalParamsHandler implements BuildRequestHandler
{

    @Override
    public void handle(HttpServletRequest request, IRequestVo requestVo) throws BizBusinessException
    {
        if (requestVo instanceof GlobalParamsAware)
        {
            // 初始化全局参数
            GlobalParams globalParams = parseGlobalParams(request);
            ((GlobalParamsAware) requestVo).setGlobalParams(globalParams);
            if (requestVo instanceof BaseRequestVo)
            {
//				((BaseRequestVo) requestVo).setUserId(globalParams.getUserId());
//				((BaseRequestVo) requestVo).setGlobalCityId((globalParams.getCityId()));
//				((BaseRequestVo) requestVo).setGlobalDepotId((globalParams.getDepotId()));
//				((BaseRequestVo) requestVo).setGlobalAddressLat(globalParams.getAddressLat());
//				((BaseRequestVo) requestVo).setGlobalAddressLon(globalParams.getAddressLon());
//				((BaseRequestVo) requestVo).setGlobalProvinceId((globalParams.getProvinceId()));
//				((BaseRequestVo) requestVo).setLevel(globalParams.getLevel());
            }
        }
    }

    private GlobalParams parseGlobalParams(HttpServletRequest httpRequest)
    {
        GlobalParams params = null;
        try {
            GlobalParams p = new GlobalParams();
//			p.setApn(writeEscapedXml(httpRequest.getParameter("apn")));
//			p.setDeviceId(writeEscapedXml(httpRequest.getParameter("deviceId")));
//			String latStr = writeEscapedXml(httpRequest.getParameter("lat"));
//			if (StringUtils.isNotEmpty(latStr)) {
//				try {
//					p.setLat(new BigDecimal(latStr));
//				} catch (Exception e) {
//				}
//			}
//			String lonStr = writeEscapedXml(httpRequest.getParameter("lon"));
//			if (StringUtils.isNotEmpty(lonStr)) {
//				try {
//					p.setLon(new BigDecimal(lonStr));
//				} catch (Exception e) {
//				}
//			}
//            p.setChannelCode(writeEscapedXml(httpRequest.getParameter("channelCode")));
//			p.setOs(writeEscapedXml(httpRequest.getParameter("os")));
//			p.setOsVersion(writeEscapedXml(httpRequest.getParameter("osVersion")));
//			p.setPartner(writeEscapedXml(httpRequest.getParameter("partner")));
//			p.setSign(writeEscapedXml(httpRequest.getParameter("sign")));
//			p.setSub(writeEscapedXml(httpRequest.getParameter("sub")));
//			p.setUserAgent(writeEscapedXml(httpRequest.getParameter("userAgent")));
//			String userIdStr = writeEscapedXml(httpRequest.getParameter("userId"));
//			if (StringUtils.isNumeric(userIdStr)) {
//				p.setUserId(Long.valueOf(userIdStr));
//			}
//			p.setVer(writeEscapedXml(httpRequest.getParameter("ver")));
//			if(p.getVer() == null || p.getVer().trim().equals("")){
//				p.setVer(writeEscapedXml(httpRequest.getParameter("version")));
//			}
//			p.setMac(writeEscapedXml(httpRequest.getParameter("mac")));
//			p.setImsi(writeEscapedXml(httpRequest.getParameter("imsi")));
//			p.setImei(writeEscapedXml(httpRequest.getParameter("imei")));
//			p.setRouterMac(writeEscapedXml(httpRequest.getParameter("routerMac")));
//			p.setStation(writeEscapedXml(httpRequest.getParameter("station")));
//			p.setAuthToken(writeEscapedXml(httpRequest.getParameter("authToken")));
//
//			String level = writeEscapedXml(httpRequest.getParameter("level"));
//			if(level == null || "".equals(level.trim())){
//				p.setLevel(1);
//			}else {
//				p.setLevel(Integer.valueOf(level));
//			}
//			String cityIdStr = writeEscapedXml(httpRequest.getParameter("cityId"));
//			if (StringUtils.isNumeric(cityIdStr)) {
//				p.setCityId(Long.valueOf(cityIdStr));
//			}
//			String provinceIdStr = writeEscapedXml(httpRequest.getParameter("provinceId"));
//			if (StringUtils.isNumeric(provinceIdStr)) {
//				p.setProvinceId(Long.valueOf(provinceIdStr));
//			}
//			String depotIdStr = writeEscapedXml(httpRequest.getParameter("depotId"));
//			if (StringUtils.isNumeric(depotIdStr)) {
//				p.setDepotId(Long.valueOf(depotIdStr));
//			}
//
//			String addressLatStr = writeEscapedXml(httpRequest.getParameter("addressLat"));
//			if (StringUtils.isNotEmpty(addressLatStr)) {
//				try {
//					p.setAddressLat(new BigDecimal(addressLatStr));
//				} catch (Exception e) {
//				}
//			}
//			String addressLonStr = writeEscapedXml(httpRequest.getParameter("addressLon"));
//			if (StringUtils.isNotEmpty(addressLonStr)) {
//				try {
//					p.setAddressLon(new BigDecimal(addressLonStr));
//				} catch (Exception e) {
//				}
//			}
            params = p;
        }
        catch (Exception e)
        {
            log.error("解析全局参数出错", e);
        }
        return params;
    }

    @Override
    public int getOrder() {
        return BuildRequestHandler.GlobalParamsHandlerOrderVaule;
    }
}
